package com.modebing.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transform.transforms.SourceConfig;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

public class ESTest_doc_search_body {

    public static void main(String[] args) throws IOException {
        // 创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest request = new SearchRequest();
        request.indices("user");
        // 查询索引当中所有的数据
        //SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //for (SearchHit hit : hits) {
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}

        // 条件查询 termQuery
        //SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30));
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //for (SearchHit hit : hits) {
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        // 分页查询
        //SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //query.from(2); // 当前页起始数据的下标
        //query.size(2);  // 当前页显示数据的大小
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        // 对查询结果排序
        //SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //query.sort("age", SortOrder.DESC);
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}

        // 过滤字段
        //SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //String[] excludes = {"age"}; // 只排除的字段
        //String[] includes = {"name"}; // 只包括的字段
        //query.fetchSource(includes,excludes);
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}



        //// 组合查询
        //SearchSourceBuilder query = new SearchSourceBuilder();
        //
        //BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //query.query(boolQueryBuilder);
        ////boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("age",40));
        ////boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("sex","男"));
        //boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("age",30));
        //boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("age",40));
        //boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery("sex","男"));
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}



        // 范围查询
        //SearchSourceBuilder query = new SearchSourceBuilder();
        //
        //RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        //rangeQuery
        //        // 大于等于
        //        .gte(30)
        //        // 小于等于
        //        .lte(50);
        //query.query(rangeQuery);
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        //// 模糊查询
        //SearchSourceBuilder query = new SearchSourceBuilder();
        //// Fuzziness.ONE:  差一个字符能匹配成功
        //// Fuzziness.TWO:  差两个字符能匹配成功
        //FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "zhangsan1").fuzziness(Fuzziness.TWO);
        //query.query(fuzziness);
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        //// 高亮查询
        //SearchSourceBuilder query = new SearchSourceBuilder();
        //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangsan");
        //query.query(termQueryBuilder);
        //HighlightBuilder highlightBuilder = new HighlightBuilder();
        //highlightBuilder.preTags("<font color='red'>");
        //highlightBuilder.postTags("<font/>");
        //highlightBuilder.field("name");
        //query.highlighter(highlightBuilder);
        //request.source(query);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        //// 聚合查询
        //SearchSourceBuilder query = new SearchSourceBuilder();
        //// maxAge 自定义字段   age   要聚合的字段
        //AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        //query.aggregation(aggregationBuilder);
        //
        //SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        //SearchHits hits = searchResponse.getHits();
        //request.source(query);
        //System.out.println(hits.getTotalHits());
        //System.out.println(searchResponse.getTook());
        //System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        //for (SearchHit hit : hits) {
        //    System.out.println(hit.getId());
        //    String asString = hit.getSourceAsString();
        //    System.out.println(asString);
        //}


        // 分组查询
        SearchSourceBuilder query = new SearchSourceBuilder();
        // ageGroup 自定义字段   age   要聚合的字段
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        query.aggregation(aggregationBuilder);
        request.source(query);
        SearchResponse searchResponse = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        request.source(query);
        System.out.println(hits.getTotalHits());
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(searchResponse.getAggregations()));
        System.out.println(searchResponse.getTook());
        System.out.printf("当前index下所有的数据总数：%s\n",hits.getTotalHits().value);
        for (SearchHit hit : hits) {
            System.out.println(hit.getId());
            String asString = hit.getSourceAsString();
            System.out.println(asString);
        }


        // 关闭客户端
        esClient.close();
    }
}
